﻿using PEPe.DataAccessModel;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.Common;
using System.Data.Entity;

namespace PEPe.ADO
{
    public class AgendaADO
    {


       public List<Agenda> Consulta(int usuarioId)
       {
          //using (var db = new PEPeEntities())
          //{
          var db = new PEPeEntities();
          var result = db.Agenda.AsQueryable().Where(x => x.Usuario.Id == usuarioId).OrderBy(x => x.DataHora).ToList<Agenda>();

          return result;
          //}

       }


       public List<Agenda> Consulta(int usuarioId, DateTime dtIni, DateTime dtFim)
       {
           //using (var db = new PEPeEntities())
           //{
           var db = new PEPeEntities();
           var result = db.Agenda.Where(x => x.Usuario.Id == usuarioId 
                                                            && x.DataHora >= dtIni 
                                                            && x.DataHora <= dtFim).OrderBy(x => x.DataHora).ToList<Agenda>();

           return result;
           //}

       }

       public Agenda ConsultaAgenda(int agendaId)
       {
           var db = new PEPeEntities();
           var result = db.Agenda.Include("Consulta").AsQueryable().Where(x => x.Id == agendaId).FirstOrDefault();

           return result;
       }

       public List<Agenda> ProximosAgendamentos(DateTime dataHora)
       {
         var db = new PEPeEntities();

         DateTime dataCompare = DateTime.Now.AddHours(Convert.ToDouble(dataHora.Hour));

         var result = db.Agenda.Include("Procedimento").Include("Usuario").AsQueryable().Where(x => x.DataHora >= DateTime.Now
                                                                                                 && x.DataHora <= dataCompare).ToList<Agenda>();

         return result;
       }

       public IEnumerable<Agenda> ListarAgendaPaginado(int? userId, string search, int skip, int take)
       {
           IEnumerable<Agenda> lista = null;
           using (var db = new PEPeEntities())
           {

               if (string.IsNullOrEmpty(search))
                 lista = (from item in db.Agenda.Include("Usuario").Include("Procedimento").Include("Consulta")
                          where item.Usuario.Id == userId
                          orderby item.DataHora
                          select item);
               else
                   lista = (from item in db.Agenda.Include("Usuario").Include("Procedimento").Include("Consulta")
                            where (item.Consulta.Descricao.ToUpper().Contains(search.ToUpper()) ||
                                   item.Usuario.Nome.ToUpper().Contains(search.ToUpper()) ||
                                   item.Procedimento.Descricao.ToUpper().Contains(search.ToUpper())) &&
                                  item.Usuario.Id == userId
                            orderby item.DataHora
                            select item);

               return lista.Skip(skip).Take(take).ToList();
           }

       }

       public int TotalRegistros(int? id, string search)
       {
           using (var db = new PEPeEntities())
           {
               int total = 0;

               if (string.IsNullOrEmpty(search))
                 total = db.Agenda.Where(x => x.Usuario.Id == id).Count();
               else
                 db.Agenda.Where(x => x.Usuario.Nome.ToUpper().Contains(search.ToUpper()) &&
                                    x.Usuario.Id == id).Count();


               return total;
           }

       }


       public void Salvar(Agenda agenda)
       {
           using (var db = new PEPeEntities())
           {
               if (agenda.Id == 0)
               {
                   db.Agenda.Add(agenda);
               }
               else
               {
                   Agenda upd = db.Agenda.Find(agenda.Id);
                   //agenda.Consulta = db.Consulta.Where(x => x.Agenda.Any(y => y.Id == agenda.Id)).FirstOrDefault();
                   db.Entry(upd).CurrentValues.SetValues(agenda);
               }
               db.SaveChanges();
           }
       }


       public void Excluir(int Id)
       {
           using (var db = new PEPeEntities())
           {
               Agenda agenda = db.Agenda.Find(Id);

               db.Agenda.Remove(agenda);
               db.SaveChanges();
           }
       }
    }
}
